<?php



class UserService extends BaseService
{
    private static $self = NULL;
    private $mUser           =  NULL;
    private $mProject        =  NULL;
    private $mBankManage     =  NULL;
    private $mMessage        =  NULL;


    public function __construct()
    {
        $this->mUser            =   new Involved_UserModel();
        $this->mProject         =   new Involved_ProjectModel();
        $this->mBankManage      =   new Involved_BankManageModel();
        $this->mMessage         =   new Involved_MessageModel();

    }
    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }

        return self::$self;
    }

    /**
     * 账户信息项目推荐
     * @return mixed
     */
    public function UserAccount()
    {
        return $this->mProject->UserAccount();
    }

    /**
     * 银行卡管理
     * @return mixed
     */
    public function UserBankcard()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mBankManage->UserBankcard($user_id);
    }

    /**
     * 银行卡绑定
     * @param $paramObj
     * @return int
     */
    public function UserBankcardBound($paramObj)
    {
        #接受信息
        $aData = [
            'bound_name'        =>$paramObj->bound_name,
            'bank_num'          =>$paramObj->bank_num,
            'bank_name'         =>$paramObj->bank_name,
            'bound_mobile'      =>$paramObj->bound_mobile,
            'user_id'           =>session_get(UserEnum::USER_INFO_USER_ID),
            'created'           =>time(),
        ];

        return $this->mBankManage->insert($aData);
    }

    /**
     * 银行卡解绑
     * @param $paramObj
     * @return int
     */
    public function UserBankcardUnbound($paramObj)
    {
        return $this->mBankManage->delete(['bank_id' => $paramObj->bank_id]);
    }

    /**
     * 投资记录
     * @return mixed
     */
    public function UserInvestTicket()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->UserInvestTicket($user_id);
    }

    /**
     * 报票记录
     * @return mixed
     */
    public function UserBTicket()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->UserBTicket($user_id);
    }

    /**
     * 账户明细
     * @return mixed
     */
    public function Account()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->Account($user_id);

    }

    /**
     * 充值
     * @return mixed
     */
    public function Recharge()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->Recharge($user_id);
    }

    /**
     * 提现
     * @return mixed
     */
    public function WithDraw()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->WithDraw($user_id);
    }

    public function RealNameAuth($paramObj)
    {
        #参数验证
        if(empty($paramObj->real_name) || empty($paramObj->id_card)){
            throw new Exception('必填项不能为空', ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }

        #身份证验证
        if (!preg_match('/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/', $paramObj->id_card)) {
            throw new Exception("身份证有误");
        }

        #身份证是否已经被绑定
        $id_card = $this->mUser->CheckCard($paramObj->id_card);
//        if ($id_card) {
//            throw new Exception("身份证已经被绑定", ProfessionErrorCodeEnum::ERROR_ACCOUNT_EXIST);
//        }

        if(!$id_card) {
            $user_id = session_get(UserEnum::USER_INFO_USER_ID);
            $status = $this->mUser->CheckCardStatus($user_id);
            if ($status != 1) {
                $url = "http://api.avatardata.cn/IdCardCertificate/VerifyAndInfo?key=42b49891a9ff402db67a91a967d3b2bd&realname=$paramObj->real_name&idcard=$paramObj->id_card&format=true";
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_TIMEOUT, 150);
                $json = curl_exec($ch);
                curl_close($ch);
                $result = json_decode($json, true);
                if ($result['error_code'] == 0) {
                    #接受信息
                    $aData = [
                        'real_name' => $paramObj->real_name,
                        'id_card' => $paramObj->id_card,
                        'status' => 1,
                    ];

                    return $this->mUser->insert($aData);
                }
            } else {
                return 1;
            }
        }else{
            return 2;
        }
    }
    /**
     * 消息列表
     */

    public function Read()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->Read($user_id);
    }

    /*public function ReadPage($limit,$page_size)
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->ReadPage($user_id,$limit,$page_size);
    }*/

    public function UnRead()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->UnRead($user_id);
    }
    public function ReadAll()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->ReadAll($user_id);
    }

    public function UserNewsDel($paramObj)
    {
       return $this->mUser->UserNewsDel($paramObj->num);
    }

//    public function AjaxPage($count,$data)
//    {
//        //总条数
//        $total = count($count);
//        //每页显示条数
//        $page_size = 10;
//        //总页数
//        $page_num = ceil($total/$page_size);
//        //获取当前页
//        $page=isset($_GET['page'])?intval($_GET['page']):1;
//        //起始位置
//        $limit = ($page-1)*$page_size;
//        //上一页
//        $last_page = $page-1>1?$page-1:1;
//        //下一页
//        $next_page = $page+1<$page_num?$page+1:$page_num;
//        $str = '';
//        foreach($data as $v){
//            $str.=  '<input type="checkbox" name="check" id="check" value="'.$v->message_id.'">';
//            $str.=  '<div class="news-title left">'.$v->title.'</div>';
//            $str.=  '<div class="news-time right">'.date('Y-m-d H:i:s',$v->created).'</div>';
//            $str.=  '<p class="news-cont">'.$v->content.'</p>';
//        }
//        for($i=1; $i<=$page_num; $i++){
//            if($page == $i){
//                $str.="<a href='javascript:void(0)' onclick=changePage(".$i.")>[".$i."]</a>&nbsp;&nbsp;&nbsp;";
//            }else{
//                $str.="<a href='javascript:void(0)' onclick=changePage(".$i.")>".$i."</a>&nbsp;&nbsp;&nbsp;";
//            }
//        }
//    }
}
